/**
 * 
 */
package com.gr.menuw.horizontal.dropdown.custom;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import com.gr.menuw.common.StringModel;
import com.gr.menuw.menu.IMenu;
import com.gr.menuw.menu.item.IMenuItem;


/**
 * @author Graham Rhodes 25 Feb 2011 22:33:11
 */
public class CustomSubMenu extends Panel implements IMenuItem, IMenu {

    private static final long serialVersionUID = 1L;
    private IMenu parentMenu;
    private final List<IMenuItem> items = new ArrayList<IMenuItem>();
    private final RepeatingView itemsView;
    private String title;
    private final Label label;

    /**
     * @param id
     */
    public CustomSubMenu(final String id, String title, IMenu parentMenu) {
        super(id);
        this.parentMenu = parentMenu;
        this.title = title;

        WebMarkupContainer link = (WebMarkupContainer) new WebMarkupContainer("link").setMarkupId(id).setOutputMarkupId(true);

        label = (Label) new Label("label", title).setOutputMarkupId(true);

        link.add(label);

        add(link);

        parentMenu.addItem(this);

        itemsView = new RepeatingView("items");
        addOrReplace(itemsView);
    }

    /* (non-Javadoc)
     * @see com.gr.menuw.menu.IMenu#isOnTop()
     */
    @Override
    public boolean isOnTop() {
        return false;
    }

    protected IModel<String> getTitleModel() {
        return new ResourceModel(getTitle(), getTitle());
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    protected void onBeforeRender() {

        itemsView.removeAll();

        for (IMenuItem item : this.items) {
            if (item instanceof Component) {
                itemsView.add((Component) item);
            }
        }

        super.onBeforeRender();
    }

    @Override
    public IMenu addItem(IMenuItem item) {
        items.add(item);
        return this;
    }

    @Override
    public IMenu removeItem(IMenuItem item) {
        items.add(item);
        return this;
    }

    /**
     * @return the parentMenu
     */
    public IMenu getParentMenu() {
        return parentMenu;
    }

    /**
     * @param parentToolbar the parentToolbar to set
     */
    public void setParentMenu(IMenu parentMenu) {
        this.parentMenu = parentMenu;
    }

    /* (non-Javadoc)
     * @see com.gr.menuw.menu.item.IMenuItem#updateLabel(java.lang.String)
     */
    @Override
    public void updateLabel(String label) {
        this.label.setDefaultModel(new StringModel(label));
    }

    /* (non-Javadoc)
     * @see com.gr.menuw.menu.item.IMenuItem#show(boolean)
     */
    @Override
    public void show(boolean visable) {
        setVisible(visable);
    }

}
